$(function () {
    function loadData() {
        $.ajax({
            type: 'get',
            url: 'http://localhost:8080/api/v1/admin/category/list',
            success: function (backData) {
                if (backData.code == 200) {
                    let data = backData.data;
                    let htmlStr = template('tpl-category', data);
                    $('tbody').html(htmlStr);
                }
            }
        });
    };
    $('#myModal').on('show.bs.modal', function (event) {
        if ($(event.relatedTarget).text() == '新增分类') {
            $('.modal-title').html('新增分类');
            $('.btn-save').html('新增');
            $('#recipient-name').val('');
            $('#message-text').val('');
        } else {
            $('.modal-title').html('编辑分类');
            $('.btn-save').html('编辑');
            window.id = $(event.relatedTarget).attr('data-id');
            $.ajax({
                type: 'get',
                url: 'http://localhost:8080/api/v1/admin/category/search',
                data: {
                    id: id,
                },
                success: function (backData) {
                    if (backData.code == 200) {
                        $('#recipient-name').val(backData.data[0].name);
                        $('#message-text').val(backData.data[0].slug);
                    }
                }
            })
        }
    });


    $('.btn-save').on('click', function () {
        if ($(this).html() == '新增') {
            let name = $('#recipient-name').val().trim();
            let slug = $('#message-text').val().trim();
            if (name == '' || slug == '') {
                alert('输入内容请不要为空');
                return;
            }
            $.ajax({
                type: 'post',
                url: 'http://localhost:8080/api/v1/admin/category/add',
                data: {
                    name: name,
                    slug: slug,
                },
                success: function (backData) {
                    if (backData.code == 201) {
                        $('#myModal').modal('hide');
                        loadData();
                    }
                }
            });
        } else {
            let name = $('#recipient-name').val().trim();
            let slug = $('#message-text').val().trim();
            if (name == '' || slug == '') {
                alert('输入内容请不要为空');
                return;
            }
            $.ajax({
                type: 'post',
                url: 'http://localhost:8080/api/v1/admin/category/edit',
                data: {
                    name: name,
                    slug: slug,
                    id: id,
                },
                success: function (backData) {
                    if (backData.code == 200) {
                        $('#myModal').modal('hide');
                        loadData();
                    }
                }
            });
        }
    });


    $('tbody').on('click', '.btn-del', function () {
        let id = $(this).attr('data-id');
        $.ajax({
            type: 'post',
            url: 'http://localhost:8080/api/v1/admin/category/delete',
            data: {
                id: id,
            },
            success: function (backData) {
                if (backData.code == 204) {
                    alert('删除成功!!!');
                    loadData();
                }
            }
        })
    })





    loadData();
})